package vip.liux.front.application.serviceimpl.chat;

import org.springframework.stereotype.Service;
import vip.liux.front.application.service.chat.IContactAppService;
import vip.liux.front.application.service.chat.dtos.users.ChatContactDto;
import vip.liux.front.application.service.chat.dtos.users.GetContactsInput;
import vip.liux.front.domain.enums.ChatMessageSide;
import vip.liux.front.domain.models.chat.IChatConversationRepository;
import vip.liux.front.domain.models.chat.IChatUserLookupService;
import vip.liux.users.IUserData;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ContactAppService implements IContactAppService {

    public static final int OTHER_CONTACT_LIMIT_PER_REQUEST = 10;

    private final IChatUserLookupService chatUserLookupService;
    private final IChatConversationRepository conversationRepository;

    public ContactAppService(IChatUserLookupService chatUserLookupService, IChatConversationRepository conversationRepository) {
        this.chatUserLookupService = chatUserLookupService;
        this.conversationRepository = conversationRepository;
    }

    /**
     * 获取联系人.
     *
     * @param input 输入参数
     * @return 联系人
     */
    @Override
    public List<ChatContactDto> getContacts(GetContactsInput input, Long userId) {
        // 获取会话联系人
        List<ChatContactDto> conversationContacts = conversationRepository
                .getListByUserId(userId, input.filter())
                .stream().map(x -> new ChatContactDto(
                        x.targetUser().getId(),
                        x.targetUser().getName(),
                        x.targetUser().getSurname(),
                        x.targetUser().getUsername(),
                        ChatMessageSide.values()[x.conversation().getLastMessageSide()],
                        x.conversation().getLastMessage(),
                        x.conversation().getLastMessageDate(),
                        x.conversation().getUnreadMessageCount()
                )).collect(Collectors.toCollection(ArrayList::new));


        if (input.includeOtherContacts()) { // 包含其他联系人

            // 查询其他联系人
            List<IUserData<Long>> lookupUsers = chatUserLookupService.searchUsers("username", input.filter(), OTHER_CONTACT_LIMIT_PER_REQUEST, 0);

            List<ChatContactDto> lookupContacts = lookupUsers
                    .stream()
                    .filter(x ->
                            conversationContacts.stream().noneMatch(c -> Objects.equals(c.userId(), x.getId()))
                                    || Objects.equals(x.getId(), userId)
                    ) // 过滤已存在的联系人
                    .map(x -> new ChatContactDto(
                            null,
                            x.getName(),
                            x.getSurname(),
                            x.getUsername(),
                            null,
                            null,
                            null,
                            0
                    )).toList();

            conversationContacts.addAll(lookupContacts);
        }
        return conversationContacts;
    }

    /**
     * 获取未读消息总数.
     *
     * @return 未读消息总数
     */
    @Override
    public int getTotalUnreadMessageCount(Long userId) {
        return conversationRepository.getTotalUnreadMessageCount(userId);
    }
}
